package com.hosever.coreTest2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class PoiTest {
	public static void main(String[] args) {
		
		File file = new File("D:\\heshihua\\网办\\南昌\\2015年10月12日责任清单数据\\test.xls");
		FileInputStream fis = null;
		
		HSSFWorkbook workbook = null;
		HSSFSheet sheet = null;
		try {
			fis = new FileInputStream(file);
			workbook = new HSSFWorkbook(fis);//读取excel到workbook里，相当于打开excel
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(workbook!=null){
			System.out.println("sheet个数：" + workbook.getNumberOfSheets());
			for(int k = 0; k < workbook.getNumberOfSheets();k++){
				sheet = workbook.getSheetAt(k);
				System.out.println("sheet名称："+sheet.getSheetName());
				System.out.println("行数：" + sheet.getLastRowNum());
				int totalColumn = 0;
				if(sheet != null){
					for (int i = 2; i <= sheet.getLastRowNum(); i++) {// 第三行开始，读取每一行
						HSSFRow row = sheet.getRow(i);
						totalColumn = row.getLastCellNum();
						List<String> cols = new ArrayList<String>();
						for (int j = 0; j < totalColumn; j++) {
							HSSFCell cell = row.getCell(j);// 读取每一格
							cols.add(cell.toString());
						}
						System.out.println(cols);
					}
				}
			}
		}
		
	}
}
